package Chapter9;

import Chapter6.WebTable.CompareImage.CompareUtil;
import Chapter6.WebTable.WebElementExtender;
import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
import static org.junit.Assert.*;

import java.io.File;

/**
 * Created by m.zabarylo on 10/13/2014.
 */
public class HTML5CanvasDrawing {
    private WebDriver driver;
    private StringBuffer verificationError = new StringBuffer();

    @Before
    public void setUp(){
        driver = new FirefoxDriver();
        driver.get("http://dl.dropbox.com/u/55228056/html5canvasdraw.html");
        driver.manage().window().maximize();
    }
    @Test
    public void testHTML5CanvasDrawing() throws Exception
    {
        //get the HTML5 Canvas element
        WebElement canvas = driver.findElement(By.id("imageTemp"));
        //Select the Pencil tool
        Select drawtool = new Select(driver.findElement(By.id("dtool")));
        drawtool.selectByValue("pencil");

        //create a action Chain for Draw a shape on Canvas
        Actions builder = new Actions(driver);
        builder.clickAndHold(canvas).moveByOffset(10, 50)
                .moveByOffset(50, 10)
                .moveByOffset(-10, -50)
                .moveByOffset(-50, -10).release().perform();

        FileUtils.copyFile(WebElementExtender.captureElementBitmap(canvas), new File("D:\\ZabaryloMaksym\\post.png"));
       /* assertEquals(CompareUtil.Result.Matched, CompareUtil.CompareImage("D:\\ZabaryloMaksym\\base_post.png\"","D:\\ZabaryloMaksym\\post.png\""));
    */
    }

    @After
    public void tearDown()
    {
        //driver.quit();
        String verificationErrorString = verificationError.toString();
        if (!"".equals(verificationErrorString))
        {
            fail(verificationErrorString);
        }
    }
}
